﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using eLearning.DosenView;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;

namespace eLearning.Controls
{
    public partial class UploadedBrowser : PhoneApplicationPage
    {
        static PhoneApplicationPage callerPage;

        string lastDirectory = "";

        public UploadedBrowser()
        {
            InitializeComponent();

            GetListContents("");
        }

        public static void CallerPage(PhoneApplicationPage page)
        {
            callerPage = page;
        }

        private void GetListContents(string folder)
        {
            string url = Config.Instance.UPLOAD_SERVICE_URL() + Config.VIEW_ACTION_KEY;

            string urlformat = string.Format("{0}&{1}&{2}", url, "folder=" + folder, Guid.NewGuid().ToString());
            Uri uri = new Uri(urlformat, UriKind.Absolute);

            WebClient webClient = new WebClient();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listUploaded_UploadStringCompleted);
            webClient.UploadStringAsync(uri, "POST", "");
        }

        private void listUploaded_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                ContentsFileListBox.Items.Clear();

                DirectoryServerList list = JsonConvert.DeserializeObject<DirectoryServerList>(e.Result);
                
                foreach (DirectoryServer dir in list)
                {
                    if ("Folder".Equals(dir.Type))
                        dir.Gambar = "../Images/appbar.journal.png";
                    else
                        dir.Gambar = "../Images/appbar.book.open.png";
                    ContentsFileListBox.Items.Add(dir);                
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        
        private void ContentsFileListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DirectoryServer file = (DirectoryServer)e.AddedItems[0];

                if ("Folder".Equals(file.Type))
                {
                    GetListContents(file.Content);
                }
                else
                {
                    NavigationService.GoBack();
                    ResourcesFormPage resources = (ResourcesFormPage)callerPage;
                    resources.selectedFile = Config.Instance.UPLOAD_URL + file.Content;
                }
            }
        }

        private void buttonUp_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            GetListContents(lastDirectory);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void backButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void GridUp_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            GetListContents(lastDirectory);
        }

    }

    public class DirectoryServer
    {
        public string Content { get; set; }
        public string Type { get; set; }
        public string Gambar { get; set; }
    }

    public class DirectoryServerList : ObservableCollection<DirectoryServer> { }
}